import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  // 前端页面路由
  {
    path: "/",
    component: () => import("@/layout/AppFrame.vue"),
    redirect: "/home",
    children: [
      {
        path: "/home",
        name: "home",
        component: () => import("@/views/HomeView.vue"),
        meta: { title: "FY's Blog" }
      },
      {
        path: "/archieves",
        name: "archieves",
        component: () => import("@/views/ArchieveView.vue"),
        meta: { title: "Archieves" }
      },
      {
        path: "/archieves/:id",
        name: "archieves-detail",
        component: () => import("@/views/ArchieveDetailView.vue"),
      },
      {
        path: "/categories",
        name: "categories",
        component: () => import("@/views/CategoryView.vue"),
        meta: { title: "Categories" }
      },
      {
        path: "/categories/:id",
        name: "categories-detail",
        component: () => import("@/views/CategoryDetailView.vue"),
      },
      {
        path: "/tags",
        name: "tags",
        component: () => import("@/views/TagsView.vue"),
        meta: { title: "Tags" }
      },
      {
        path: "/tags/:id",
        name: "tags-detail",
        component: () => import("@/views/TagsDetailView.vue"),
      },
      {
        path: "/about",
        name: "about",
        // route level code-splitting
        // this generates a separate chunk (About.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import("@/views/AboutView.vue"),
        meta: { title: "About" }
      },
      {
        path: "/detail/:id",
        name: "detail",
        component: () => import("@/views/BlogDetailView.vue"),
      },
    ],
  }
];

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_BASE_URL),
  routes,
  scrollBehavior(to, from, savedPosition) {
    // 在按下前进和后退按钮时，保存滚动位置
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0, behavior: "auto" } 
    }
  },
});

export default router
